package com.zyzd.pathdemo.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;

import com.zyzd.pathdemo.R;

/**
 * Created by 李宗源 on 2017/5/3.
 */

public class ShaderView extends View {
    public Paint mPaint;
    public Bitmap mBitmap;

    public ShaderView(Context context) {
        this(context, null);
        init();
    }


    public ShaderView(Context context, AttributeSet attrs) {
        this(context, null, 0);
    }


    public ShaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    /**
     * Shader.TileMode.CLAMP:
     */
    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_unstatement);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int bitMapWidth = mBitmap.getWidth();

        int bitmapHeight = mBitmap.getHeight();
        //----------------------------------------BitmapShader----------------------------------------

        BitmapShader bitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.MIRROR);
        mPaint.setShader(bitmapShader);
        canvas.drawRect(0, 0, bitMapWidth * 4, bitmapHeight * 4, mPaint);

        bitmapShader = new BitmapShader(mBitmap, Shader.TileMode.MIRROR, Shader.TileMode.MIRROR);
        mPaint.setShader(bitmapShader);
        canvas.drawRect(bitMapWidth * 4 + 50, 0, bitMapWidth * 8 + 50, bitmapHeight * 4, mPaint);

        bitmapShader = new BitmapShader(mBitmap, Shader.TileMode.REPEAT, Shader.TileMode.MIRROR);
        mPaint.setShader(bitmapShader);
        canvas.drawRect(bitMapWidth * 8 + 100, 0, bitMapWidth * 12 + 100, bitmapHeight * 4, mPaint);


        //----------------------------------------LinearGradient----------------------------------------

        int height = bitmapHeight * 4 + 50;

        LinearGradient linearGradient = new LinearGradient(0, height, 50, height + 50, new int[]{
                Color.RED, Color.YELLOW, Color.BLACK, Color.WHITE},
                new float[]{0, .1F, .8F, .9F}, Shader.TileMode.REPEAT);// positions:渐变的相对位置,为空时均匀分布

        mPaint.setShader(linearGradient);
        canvas.drawRect(0, height, bitMapWidth * 4, height + bitMapWidth * 4, mPaint);


        linearGradient = new LinearGradient(bitMapWidth * 4 + 50, height, bitMapWidth * 4 + 100, height + 50,
                new int[]{Color.RED, Color.YELLOW, Color.BLACK, Color.WHITE},
                null, Shader.TileMode.MIRROR);
        mPaint.setShader(linearGradient);
        canvas.drawRect(bitMapWidth * 4 + 50, height, bitMapWidth * 8 + 50, height + bitMapWidth * 4, mPaint);

        linearGradient = new LinearGradient(bitMapWidth * 8 + 100, height, bitMapWidth * 8 + 150, height + 50,
                new int[]{Color.RED, Color.YELLOW, Color.BLACK, Color.WHITE},
                null, Shader.TileMode.CLAMP);
        mPaint.setShader(linearGradient);
        canvas.drawRect(bitMapWidth * 8 + 100, height, bitMapWidth * 12 + 100, height + bitMapWidth * 4, mPaint);

        linearGradient = new LinearGradient(bitMapWidth * 12 + 150, height, bitMapWidth * 12 + 200, height + 50, Color.RED,
                Color.YELLOW, Shader.TileMode.MIRROR);
        mPaint.setShader(linearGradient);
        canvas.drawRect(bitMapWidth * 12 + 150, height, bitMapWidth * 16 + 150, height + bitMapWidth * 4, mPaint);

        //----------------------------------------RadialGradient----------------------------------------

        height = height + bitMapWidth * 4 + 50;

        RadialGradient radialGradient = new RadialGradient(bitMapWidth * 2, height + bitMapWidth * 2, bitMapWidth * 2, new int[]{
                Color.YELLOW, Color.RED, Color.BLUE, Color.GREEN}, null,
                Shader.TileMode.CLAMP);

        mPaint.setShader(radialGradient);
        canvas.drawRect(0, height, bitMapWidth * 4, height + bitMapWidth * 4, mPaint);

        radialGradient = new RadialGradient(bitMapWidth * 6 + 50, height + bitMapWidth * 2, bitMapWidth * 2, new int[]{
                Color.YELLOW, Color.RED, Color.BLUE, Color.GREEN}, null,
                Shader.TileMode.MIRROR);

        mPaint.setShader(radialGradient);
        canvas.drawRect(bitMapWidth * 4 + 50, height, bitMapWidth * 8 + 50, height + bitMapWidth * 4, mPaint);

        radialGradient = new RadialGradient(bitMapWidth * 10 + 100, height + bitMapWidth * 2, bitMapWidth * 2, new int[]{
                Color.YELLOW, Color.RED, Color.BLUE, Color.GREEN}, null,
                Shader.TileMode.REPEAT);

        mPaint.setShader(radialGradient);
        canvas.drawRect(bitMapWidth * 8 + 100, height, bitMapWidth * 12 + 100, height + bitMapWidth * 4, mPaint);

        //----------------------------------------SweepGradient----------------------------------------
        height = height + bitMapWidth * 4 + 50;

        SweepGradient sweepGradient = new SweepGradient(bitMapWidth * 2, bitMapWidth * 2 + height, new int[]{Color.YELLOW,
                Color.RED, Color.BLUE, Color.GREEN}, new float[]{0, .2F, .6F, .9F});

        mPaint.setShader(sweepGradient);
        canvas.drawRect(0, height, bitMapWidth * 4, bitMapWidth * 4 + height, mPaint);

        sweepGradient = new SweepGradient(bitMapWidth * 6 + 50, bitMapWidth * 2 + height, new int[]{Color.YELLOW,
                Color.RED, Color.BLUE, Color.GREEN}, new float[]{0, .2F, .6F, .9F});

        mPaint.setShader(sweepGradient);
        canvas.drawCircle(bitMapWidth * 6 + 50, bitMapWidth * 2 + height, bitMapWidth * 2, mPaint);

        //----------------------------------------ComposeShader----------------------------------------
        height = height + bitMapWidth * 4 + 50;

        sweepGradient = new SweepGradient(bitMapWidth * 2, bitMapWidth * 2 + height, new int[]{Color.YELLOW,
                Color.RED, Color.BLUE, Color.GREEN}, new float[]{0, .2F, .6F, .9F});
        ComposeShader composeShader = new ComposeShader(bitmapShader, sweepGradient,
                PorterDuff.Mode.DARKEN);
        mPaint.setShader(composeShader);
        canvas.drawRect(0, height, bitMapWidth * 4, bitMapWidth * 4 + height, mPaint);



    }
}

